<!--
 * @Author: guo wei
 * @LastEditors: [guo wei]
 * @LastEditTime: 2024-01-08 19:55:25
 * @Description: 
-->
<template>
  <div class="color-wrap">
    <el-button type="primary" @click="handleAdd">新增色系</el-button>
    <el-table
      :data="tableData"
      style="width: 800px; margin-top: 20px"
      row-key="id"
      border
      default-expand-all
    >
      <el-table-column prop="colorSystemName" label="色系">
        <template #default="scope">
          {{ scope.row.colorSystemName }}
        </template>
      </el-table-column>
      <el-table-column prop="colorSystemValue" label="" width="150">
        <template #default="scope">
          <div
            class="color-system-tag"
            :style="{ 'background-color': '#' + scope.row.colorSystemValue }"
          ></div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      v-model="dialogVisible"
      :title="dialogTitle"
      width="30%"
    >
      <div>
        <el-form
          ref="ruleFormRef"
          :model="ruleForm"
          :rules="rules"
          label-width="120px"
          class="demo-ruleForm"
        >
          <el-form-item label="色系名称" prop="name">
            <el-input v-model="ruleForm.name" />
          </el-form-item>
          <el-form-item label="色系" prop="color">
            <el-color-picker v-model="ruleForm.color" />
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="confirmCreateType">
            确认
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref, reactive } from "vue";
import {
  getColorSystemInfos,
  addColorSystemInfo,
  modifyColorSystemInfo,
  deleteColorSystemInfo,
} from "@/api/building-type";
import { ElMessage, ElMessageBox } from "element-plus";
const dialogVisible = ref(false);
const ruleFormRef = ref();
const ruleForm = reactive({
  color: "",
  name: "",
});
const dialogTitle = ref("添加色系");
const dialogType = ref("add");
const currentRowId = ref("");
const handleEdit = (index, row) => {
  console.log(index, row);
  dialogVisible.value = true;
  dialogTitle.value = "编辑色系";
  dialogType.value = "edit";
  currentRowId.value = row;
  ruleForm.color = "#" + row.colorSystemValue;
  ruleForm.name = row.colorSystemName;
};
const handleAdd = () => {
  dialogVisible.value = true;
  dialogTitle.value = "添加色系";
  dialogType.value = "add";
  currentRowId.value = "";
};
const tableData = ref([]);
const getListData = async () => {
  const params = { count: true, pageId: 1, pageSize: 20 };
  try {
    const res = await getColorSystemInfos(params);
    if (res.code == 200) {
      // let temp =
      //   res.result.list.map(async (x) => {
      //     let colorImage = "";
      //     if (x.colorImage) {
      //       const res = await getFilesUrl(x.colorImage);
      //       colorImage = res;
      //     }
      //     let colorSystemValue = "";
      //     if (x.colorRelVOLst) {
      //       colorSystemValue =
      //         x.colorRelVOLst.map((x) => x.colorSystemValue) || [];
      //     }
      //     return {
      //       ...x,
      //       colorImage,
      //       colorSystemValue: colorSystemValue.join(","),
      //     };
      //   }) || [];
      tableData.value = res.result.list || [];
      console.log("tabledata", res);
    }
  } catch (error) {}
};
getListData();
const handleDelete = (index, row) => {
  ElMessageBox.confirm("确认删除吗?", "Warning", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      const res = await deleteColorSystemInfo({
        colorSystemInfoId: row.colorSystemInfoId,
        recordVersion: row.recordVersion,
      });
      if (res.code == 200) {
        ElMessage({
          type: "success",
          message: "删除成功",
        });
        getListData();
      }
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消删除",
      });
    });
};
const confirmCreateType = async () => {
  if (!ruleFormRef.value) return;
  await ruleFormRef.value.validate(async (valid, fields) => {
    if (valid) {
      if (dialogType.value == "add") {
        try {
          const res = await addColorSystemInfo({
            colorSystemName: ruleForm.name,
            colorSystemValue: ruleForm.color.replace("#", ""),
          });
          if (res.code == 200) {
            dialogVisible.value = false;
            getListData();
          }
        } catch (error) {}
      } else {
        try {
          const res = await modifyColorSystemInfo({
            colorSystemName: ruleForm.name,
            colorSystemValue: ruleForm.color.replace("#", ""),
            colorSystemInfoId: currentRowId.value.colorSystemInfoId,
            recordVersion: currentRowId.value.recordVersion,
          });
          if (res.code == 200) {
            dialogVisible.value = false;
            getListData();
          }
        } catch (error) {}
      }
    } else {
      console.log("error submit!", fields);
    }
  });
};
</script>
<style lang="scss" scoped>
.card-wrap {
  margin: 20px;
}
.color-system-tag {
  width: 25px;
  height: 25px;
  margin: 0 auto;
  border: 1px solid #409eff;

  margin-left: 5px;
}
</style>
